package com.learn.support;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;

/**
 * Description
 * Copyright © 启明星辰 版权所有
 *
 * @author chenjiquan
 * @date 2024-08-05
 */
public class ZhouyuImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {

		Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ZhoyuMapperScan.class.getName());
		String path = (String) annotationAttributes.get("value");

		// 利用spring的类扫描器来帮我扫描注册，不需要自己重新写扫描注册逻辑
		ZhouyuBeanDefinitionScanner scanner = new ZhouyuBeanDefinitionScanner(registry);
		scanner.addIncludeFilter((metadataReader, metadataReaderFactory) -> true);
		// 扫描注册
		scanner.doScan(path);
	}
}
